// pages/dish/index/index.js
const app=getApp();
const server = require('../../../utils/server');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    mellName: "",
    youhuiquan: {},
    storeName:'',
    storeData: {},
    storeCoupons: [],
    storeGoodTree: []
  },

  isAuth(){
    let that=this;
    wx.getSetting({
      withSubscriptions: true,
      success(res){
        if(!res.authSetting['scope.userLocation']){
          wx.showModal({
            title:'提示',
            content:'请求获取位置权限',
            success(res){
              if(res.confirm==false){
                return false
              }else{
                that.yesAuth();
              }
            }
          })
        }else{
          that.yesAuth();
        }
      }
    })
  },

  yesAuth: function() {
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy: true,
      success: function(res) {
        console.log(res) // 获取到经纬度信息
        let location = {
          latitude: res.latitude,
          longitude: res.longitude
        }
        that.getStoreInfo(location)
        wx.setStorageSync('location', location)
      },
      fail: function(err) {
        console.log(err)
      }
    })
  },

  goMell(){
    wx.navigateTo({
      url: '/pages/dish/mell/mell',
    })
  },

  // getStoreInfo(location){
  //   server.requestForm(false,'get','location',location,function(res){
  //     console.log(res)
  //   })

  //   // server.requestForm(false, 'get', 'mall', {}, function(res) {
  //   //   console.log("1231231231231231223", res.data[0]);
  //   // });
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let storeName=wx.getStorageSync('storeName');
    this.setData({
      storeName:storeName
    })
    this.getStoreInfo();
    this.getCoupon();
    this.getGoodTree();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let tabbar=this.selectComponent("#tabbar");
    tabbar.setData({
      selected:0
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getStoreInfo() {
    let that=this;
    server.requestForm(true, "get", "store", { "filter[_id]": wx.getStorageSync('toStoreInfoPageId') }, (res) => {
     if(res.code == 0){
      
     }
    })
  },

  getCoupon() {
    server.requestForm(true, "get", "coupon", {
      "filter": {
        "storeId": [wx.getStorageSync('toStoreInfoPageId')]
      }
    }, (res) => {
      this.setData({
        storeCoupons: res.data
      });
    });
  },

  getGoodTree() {
    server.requestForm(true, "get", "goodsType/tree", {
      storeId: wx.getStorageSync('toStoreInfoPageId')
    }, (res) => {
      console.log("getGoodTree", res);
      this.setData({
        storeGoodTree: res.data
      });
    })
  }
})